package com.codefish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codefish.entity.OutMoneyClass;
import com.codefish.entity.OutMoneyType;
import com.codefish.mapper.OutMoneyTypeDao;
import com.codefish.service.OutMoneyClassService;
import com.codefish.service.OutMoneyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service("outMoneyTypeService")
public class OutMoneyTypeServiceImpl extends ServiceImpl<OutMoneyTypeDao, OutMoneyType> implements OutMoneyTypeService {


    @Autowired
    private OutMoneyClassService classService;

    /**
     * 新增单个
     *
     * @param outMoneyType
     * @return
     */
    @Override
    public Integer saveOne(OutMoneyType outMoneyType) {

        boolean flag = this.save(outMoneyType);
        if (!flag) {
            return null;
        }
        Integer id = outMoneyType.getId();

        return id;
    }

    /**
     * 修改单个
     *
     * @param outMoneyType
     * @return
     */
    @Override
    public boolean updateOne(OutMoneyType outMoneyType) {

        boolean flag = this.updateById(outMoneyType);

        return flag;
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteOne(Integer id) {
        OutMoneyType outMoneyType = new OutMoneyType();
        outMoneyType.setId(id);
        outMoneyType.setIsDel(1);

        boolean flag = this.updateById(outMoneyType);

        return flag;
    }

    /**
     * 分页条件查询
     *
     * @param familyId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage<OutMoneyType> getPageByParams(Integer familyId, Integer pageNo, Integer pageSize) {

        Page<OutMoneyType> page = new Page<>(pageNo, pageSize);

        QueryWrapper<OutMoneyType> qw = new QueryWrapper<>();
        qw.eq("state", 1).ne("is_del", 1);
        qw.eq("family_id", familyId);
        qw.orderByDesc("id");

        IPage<OutMoneyType> retPage = baseMapper.selectPage(page, qw);

        this.setNameByCode(retPage);


        return retPage;
    }

    /**
     * setNameByCode
     *
     * @param retPage
     */
    private void setNameByCode(IPage<OutMoneyType> retPage) {

        List<Integer> classList = retPage.getRecords()
                .stream()
                .filter(one -> !ObjectUtils.isEmpty(one.getOutMoneyClass()))
                .map(OutMoneyType::getOutMoneyClass)
                .collect(Collectors.toList());

        List<OutMoneyClass> outMoneyClassList = classService.selectList(classList);

        for (OutMoneyType outMoneyType : retPage.getRecords()) {
            if(ObjectUtils.isEmpty(outMoneyType.getOutMoneyClass())){
                continue;
            }

            Integer outMoneyClassId = outMoneyType.getOutMoneyClass();

            OutMoneyClass outMoneyClass = outMoneyClassList
                    .stream()
                    .filter(one -> one.getId().equals(outMoneyClassId))
                    .findFirst()
                    .orElse(null);

            assert outMoneyClass != null;
            outMoneyType.setOutMoneyClassName(StringUtils.isEmpty(outMoneyClass.getOutMoneyClassName()) ? null : outMoneyClass.getOutMoneyClassName());
        }
    }

    /**
     * 根据id批量查询
     *
     * @param outMoneyTypeList
     * @return
     */
    @Override
    public List<OutMoneyType> selectListByIds(List<Integer> outMoneyTypeList) {

        QueryWrapper<OutMoneyType> qw = new QueryWrapper<>();
        qw.in("id", outMoneyTypeList);

        return baseMapper.selectList(qw);
    }
}